#include "heap.h"

int compare(ElemType a, ElemType b) { return b - a; }

void test1() {
    Heap* heap = createHeap();
    int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int n = sizeof(data) / sizeof(*data);
    for (int i = 0; i < n; ++i) {
        insert(heap, data[i]);
    }
    for (int i = 0; i < n; ++i) {
        printf("%d ", extract(heap));
    }
}

void test2() {
    Heap* heap = createHeapWithCompare(compare);
    int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int n = sizeof(data) / sizeof(*data);
    for (int i = 0; i < n; ++i) {
        insert(heap, data[i]);
    }
    for (int i = 0; i < n; ++i) {
        printf("%d ", extract(heap));
    }
}

void print_arr(int* arr, int n) {
    printf("[");
    for (int i = 0; i < n; ++i) {
        printf("%d ", arr[i]);
    }
    printf("]\n");
}

void test3() {
    int data[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
    int n = sizeof(data) / sizeof(*data);
    heapSort(data, n);
    printf("before heapSort: ");
    print_arr(data, n);
    printf("after heapSort: ");
    print_arr(data, n);
}

void test4() {
    int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int n = sizeof(data) / sizeof(*data);
    heapSortWithCompare(data, n, compare);
    printf("before heapSortWithCompare: ");
    print_arr(data, n);
    printf("after heapSortWithCompare: ");
    print_arr(data, n);
}

void test5() {
    Heap* heap = createHeap();
    insert(heap, 1);
    insert(heap, 4);
    insert(heap, 6);
    insert(heap, 5);
    insert(heap, 2);
    insert(heap, 3);
    insert(heap, 7);
    insert(heap, 8);
    insert(heap, 9);
    insert(heap, 10);
    int size = heapSize(heap);
    printf("[");
    for (int i = 0; i < size; ++i) {
        printf(i == size - 1 ? "%d" : "%d, ", heap->data[i]);
    }
    printf("]\n");
}

int main() {
    void (*test[])() = {test1, test2, test3, test4, test5};
    int n = sizeof(test) / sizeof(*test);

    for (int i = 0; i < n; ++i) {
        printf("test %d ------------------\n", i + 1);
        test[i]();
        printf("\n");
    }
    return 0;
}